﻿using MySql.Data.MySqlClient;
using System.Text;

namespace WorkList
{
    class DB
    {
        private MySqlConnection connection;
        private MySqlCommand cmd;

        public DB() {
            connection = new MySqlConnection("server=localhost;port=3306;username=root;password=root;database=worklistdb");
            
            cmd = new MySqlCommand();
            cmd.Connection = connection;
        }
        
        public MySqlDataReader SelectAll()
        {
            cmd.CommandText = "SELECT * FROM `classes` ORDER BY `date` ASC, `time` ASC";
            return cmd.ExecuteReader();
        }

        public MySqlDataReader LessonsInYearMonth(string year, string month)
        {
            cmd.CommandText = $"SELECT * FROM `classes` WHERE `date` LIKE '{year}-{month}-%' ORDER BY `date` ASC, `time` ASC";
            return cmd.ExecuteReader();
        }
        public void openConnection()
        {
            try
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                    connection.Open();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
        public void closeConnection()
        {
            try
            {
                if (connection.State == System.Data.ConnectionState.Open)
                    connection.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

    }
}
